package com.cheng.controller;



import com.alibaba.fastjson.JSONObject;
import com.cheng.service.FaceService.FaceService;
import com.cheng.service.FaceService.TeacherService;
import com.cheng.service.FaceService.UserService;
import com.cheng.utli.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

import static com.cheng.controller.Constant.*;

@Controller
@RequestMapping(value = "/user" ,produces="text/html;charset=UTF-8")
public class UserController{


	private final String TYPE ="student,teacher";

	@Autowired
	private FaceService faceService;

	@Autowired
	private UserService userService;

	@Autowired
	private TeacherService teacherService;

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	@RequestMapping("/register")
	@ResponseBody
	public JSONObject register(HttpServletRequest request){
		JSONObject returnObj = new JSONObject();
		//获得前台传递的参数
		//注册类型 type：student teacher
		String type = request.getParameter("type");
		String userId = request.getParameter("userId");
		String name = request.getParameter("name");
		String openId = request.getParameter("openId");

		if(true){
			return setReturnObjInfo("noType","22222222222222");
		}
		//参数类型错误
		if(!TYPE.contains(type)){
			return setReturnObjInfo("noType","参数错误");
		}
		//学生注册
		if("student".equals(type)){
			//前台传入的url图片
			String faceUrl = request.getParameter("faceUrl");

			HashMap userInfo = new HashMap<String,String>();
			userInfo.put("faceUrl",faceUrl);
			userInfo.put("user_id",userId);
			userInfo.put("openId",openId);
			userInfo.put("name",name);

			String result = faceService.addFace(userInfo);
			if("hasUser".equals(result)){
				return setReturnObjInfo("hasUser","该用户已经存在");
			}

			if("noHasFace".equals(result)){
				return setReturnObjInfo("noHasFace","没有检测到人脸");
			}
			if(FACEALEAD.equals(result)){
				return setReturnObjInfo("FaceAlready","该人脸已经注册");
			}

			if("max_face_num".equals(result)){
				return setReturnObjInfo("max_face_num","照片上传人脸数量大于1");
			}
			returnObj.put("sessionId",request.getSession().getId());
			returnObj.put("resCode","100");
			returnObj.put("type","student");
			return returnObj;
		}
		//教师注册
		HashMap userInfo = new HashMap<String,String>();
		userInfo.put("user_id",userId);
		userInfo.put("openId",openId);
		userInfo.put("name",name);
		userService.addUserInfo(userInfo);
		returnObj.put("sessionId",request.getSession().getId());
		returnObj.put("resCode","100");
		returnObj.put("type","teacher");
		return returnObj;
	}


	@RequestMapping("/checkUser")
	@ResponseBody
	public JSONObject checkUser(HttpServletRequest request){
		JSONObject returnObj = new JSONObject();
		String userId = request.getParameter("userId");

		boolean flag = userService.checkUSer(userId);
		if(flag){
			returnObj.put("resCode","hasUser");
			returnObj.put("resInfo","该用户ID已存在");
			return returnObj;
		}
		returnObj.put("resCode","100");
		return returnObj;
	}

	@RequestMapping("/checkTeacher")
	@ResponseBody
	public JSONObject checkTeacher(HttpServletRequest request){
		JSONObject returnObj = new JSONObject();
		String userId = request.getParameter("userId");

		boolean flag = teacherService.checkTeacher(userId);
		if(flag){
			returnObj.put("resCode","hasUser");
			returnObj.put("resInfo","该用户ID已存在");
			return returnObj;
		}
		returnObj.put("resCode","100");
		return returnObj;
	}
	//用户登录
	@RequestMapping(value = "/loginUser",method = RequestMethod.GET)
	@ResponseBody
	public JSONObject loginUser(HttpSession session,HttpServletRequest request){
		JSONObject returnObj = new JSONObject();
		//获得用户的openid
		String openID = request.getParameter("openID");
		String result = userService.login(openID);
		boolean flag = false;
		if(Utils.isBlank(result)){
			flag = true;
		}else{
			session.setAttribute("openId",result);
			returnObj.put("resCode","100");
			returnObj.put("type","student");
			returnObj.put("resInfo","suc");
			returnObj.put("sessionId",request.getSession().getId());
			return returnObj;
		}
		result = userService.loginTeacher(openID);
		if(Utils.isBlank(result)){
			flag = true;
		}else{
			session.setAttribute("openId",result);
			returnObj.put("resCode","100");
			returnObj.put("type","teacher");
			returnObj.put("resInfo","suc");
			returnObj.put("sessionId",request.getSession().getId());
			return returnObj;
		}
		if(flag){
			returnObj.put("resCode","noRegister");
			returnObj.put("resInfo","显示注册页面");
			return returnObj;
		}
		session.setAttribute("openId",result);
		return returnObj;
	}
	//老师登录
	@RequestMapping("/loginTeacher")
	@ResponseBody
	public JSONObject loginTeacher(HttpSession session,HttpServletRequest request){
		JSONObject returnObj = new JSONObject();
		//获得用户的openid
		String openID = request.getParameter("openID");
		String result = userService.loginTeacher(openID);
		if(Utils.isBlank(result)){
			returnObj.put("resCode","noRegister");
			returnObj.put("resInfo","显示注册页面");
			return returnObj;
		}
		session.setAttribute("openId",result);
		returnObj.put("resCode","100");
		returnObj.put("resInfo","suc");
		return returnObj;
	}


	public static JSONObject setReturnObjInfo(String resCode, String resInfo) {
		JSONObject result = new JSONObject();
		result.put("resCode",resCode);
		result.put("resInfo",resInfo);
		return result;
	}
}
